package myky.com.doctorapp.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import myky.com.doctorapp.R;
import myky.com.doctorapp.entity.UMShareEntity;

public class UMShareUtils {

    private enum SHARETYPE {
        /**
         * QQ
         */
        SHARETYPEQQ,
        /**
         * 微信
         */
        SHARETYPEWECHAT,
        /**
         * 朋友圈
         */
        SHARETYPEMOMENT,
        /**
         * QQ空间
         */
        SHARETYPEQQZONE
    }

    public interface ShareStatesCallback {

        void shareSucees();

        void shareFailed(String message);

    }


    public static void showShareDialog(Activity context, UMShareEntity umShareEntity, ShareStatesCallback shareStatesCallback) {

        final Dialog dialog_window = new Dialog(context, R.style.ActionSheetDialogStyle);
        View inflate = LayoutInflater.from(context).inflate(R.layout.share_dialog_layout, null);
        dialog_window.setContentView(inflate);
        Window window = dialog_window.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.y = 10;
        layoutParams.width = SystemUtil.getMetrics(context)[0] - 20;
        window.setAttributes(layoutParams);
        dialog_window.show();

        LinearLayout weChat = inflate.findViewById(R.id.dialog_window_wxhy);
        LinearLayout weChatMoment = inflate.findViewById(R.id.dialog_window_wxpyq);
        LinearLayout qq = inflate.findViewById(R.id.dialog_window_qq);
        LinearLayout qzon = inflate.findViewById(R.id.dialog_window_qzon);


        weChat.setOnClickListener(view -> {
            shareInfoWithWeChat(umShareEntity, context,shareStatesCallback);
            dialog_window.dismiss();
        });
        weChatMoment.setOnClickListener(view -> {
            shareInfoWithweChatMoment(umShareEntity, context);
            dialog_window.dismiss();
        });
        qq.setOnClickListener(view -> {
            shareInfoWithQQ(umShareEntity, context);
            dialog_window.dismiss();
        });
        qzon.setOnClickListener(view -> {
            shareInfoWithQzone(umShareEntity, context);
            dialog_window.dismiss();
        });


    }

    private static void shareInfoWithQzone(UMShareEntity umShareEntity, Context context) {

    }

    private static void shareInfoWithQQ(UMShareEntity umShareEntity, Context context) {

    }

    private static void shareInfoWithweChatMoment(UMShareEntity umShareEntity, Context context) {

    }


    private static void shareInfoWithWeChat(UMShareEntity umShareEntity, Activity context, ShareStatesCallback shareStatesCallback) {


        if (UMShareAPI.get(context).isInstall(context, SHARE_MEDIA.WEIXIN)) {
            UMImage umImage = new UMImage(context, umShareEntity.getShareImageUrl());
            UMWeb web = new UMWeb(umShareEntity.getShareWebUrl());
            web.setTitle(umShareEntity.getShareTitle());
            web.setThumb(umImage);
            web.setDescription(umShareEntity.getShareDeascrebe());

            new ShareAction(context)
                    .setPlatform(SHARE_MEDIA.WEIXIN)
                    .withMedia(web)
                    .setCallback(new UMShareListener() {
                        @Override
                        public void onStart(SHARE_MEDIA share_media) {
                        }

                        @Override
                        public void onResult(SHARE_MEDIA share_media) {
                            shareStatesCallback.shareSucees();
                        }

                        @Override
                        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                            shareStatesCallback.shareFailed(throwable.getMessage());
                        }

                        @Override
                        public void onCancel(SHARE_MEDIA share_media) {
                        }
                    })//回调监听器
                    .share();
        } else {
            shareStatesCallback.shareFailed("请下载最新微信版本");
        }
    }

}
